package Tracker.http;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.json.JSONObject;

import com.sun.net.httpserver.*;

public class Request {
	Headers headers;
	JSONObject body;

	public Request(HttpExchange t) throws IOException, JSONException {
		this.headers = t.getRequestHeaders();

		StringWriter writer = new StringWriter();
		IOUtils.copy(t.getRequestBody(), writer, "utf-8");
		this.body = new JSONObject(writer.toString());
	}

	public String getHeader(String key) {
		return this.headers.getFirst(key);
	}

	public List<String> getHeaders(String key) {
		return this.headers.get(key);
	}

	public Headers getHeaders() {
		return this.headers;
	}

	public JSONObject getBody() {
		return this.body;
	}
}
